//
//  add_addressController.m
//  pmmSS
//
//  Created by Mac book on 2017/9/21.
//  Copyright © 2017年 WTFSSD. All rights reserved.
//

#import "add_addressController.h"
#import "LZCityPickerController.h"

@interface add_addressController ()<UITextFieldDelegate>

@property (nonatomic,strong) NSArray *dataArray;

@property (nonatomic,strong) NSString *name_str;
@property (nonatomic,strong) NSString *phone_num_str;
@property (nonatomic,strong) NSString *location_str;
@property (nonatomic,strong) NSString *location_detail_str;

@property (nonatomic,strong) UILabel *location_label;


//@property (nonatomic,strong) UITextView *textView;

@end

@implementation add_addressController

- (void)viewDidLoad {

    NSLog(@"我的界面");

    [super viewDidLoad];
    _dataArray = @[ @[@"收件人",@"联系电话",@"所在地区",@"详细地址"],@[@"姓名",@"电话号码",@"",@"例：5号楼203室"]];



    [self setTableView:self.tableView];


}
-(void)viewDidAppear:(BOOL)animated{

    [super viewDidAppear:animated];

    [self setUpNavBar];

  

}
-(void)setUpNavBar{


    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"nav_bgimage"] forBarMetrics:UIBarMetricsDefault];
    self.title = @"新增地址";
    self.titleColor = [UIColor whiteColor];

    // TODO :导航左按钮

    UIButton *leftBtn = [HMButton createButtonWithTitle:@"" andTitleColor:nil andBgColor:nil andImage:[UIImage imageNamed:@"icon_arrow_left_white"] andBgImage:nil andSelecter:@selector(clickButton:) andTarget:self andTag:101];
    leftBtn.frame = CGRectMake(0, 0, autoScaleW(60), autoScaleW(30));
    [leftBtn setImageEdgeInsets:UIEdgeInsetsMake(autoScaleH(7), 0, autoScaleH(7), autoScaleW(50))];
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithCustomView:leftBtn];

    [self.navigationItem setLeftBarButtonItem:leftItem];
    // TODO :导航右按钮
    UIButton *rightBtn = [HMButton createButtonWithTitle:@"确定" andTitleColor:[UIColor whiteColor] andBgColor:nil andImage:nil andBgImage:nil andSelecter:@selector(clickButton:) andTarget:self andTag:102];
    rightBtn.frame = CGRectMake(0, 0, 70, 30);
    rightBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 30, 0, 0);
    rightBtn.titleLabel.font = [UIFont fontWithName:default_fontname size:autoScaleW(16)];

    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithCustomView:rightBtn];
    [self.navigationItem setRightBarButtonItem:rightItem];



}
-(void)setUpTableView:(UITableView *)tableView{


    tableView.tableFooterView.backgroundColor = View_BackgroundColor;
    tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);

    [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"];


}

#pragma mark : UITableViewDelegate  &&   UITableViewDataSource


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return 4;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return 1;


}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString * cellID = @"UITableViewCell" ;

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];

    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    UILabel *label = [HMLabel createLabelWithText:_dataArray[0][indexPath.section] andTextColor:RGBA(51,51,51,1) andBgColor:nil andFont:[UIFont fontWithName:default_fontname size:autoScaleW(14)] andTextAlignment:NSTextAlignmentLeft];

    [cell.contentView addSubview:label];



    [label mas_makeConstraints:^(MASConstraintMaker *make) {

        make.centerY.mas_equalTo(cell.contentView);
        make.left.mas_equalTo(autoScaleW(20));

        make.height.mas_equalTo(autoScaleH(17));

        make.width.mas_equalTo(autoScaleW(60));

    }];


    if (indexPath.section == 2) {

        UIImageView *rightImage = [[UIImageView alloc]init];

        [rightImage setImage:[UIImage imageNamed:@"icon_arrow_right_gray"]];
        [cell.contentView addSubview:rightImage];

        [rightImage mas_makeConstraints:^(MASConstraintMaker *make) {

            make.centerY.mas_equalTo(cell.contentView);
            make.right.mas_equalTo(cell.contentView).offset(autoScaleW(-10));

            make.height.mas_equalTo(autoScaleW(18));
            
            make.width.mas_equalTo(autoScaleH(9));
            
        }];


        //  label

        _location_label = [HMLabel createLabelWithText:nil andTextColor:RGBA(51,51,51,1) andBgColor:nil andFont:[UIFont fontWithName:default_fontname size:autoScaleW(14)] andTextAlignment:NSTextAlignmentLeft];

        [cell.contentView addSubview:_location_label];

        [_location_label mas_makeConstraints:^(MASConstraintMaker *make) {

            make.centerY.mas_equalTo(cell.contentView);
            make.left.mas_equalTo(autoScaleW(90));

            make.height.mas_equalTo(autoScaleH(17));

            make.right.mas_equalTo(autoScaleW(-20));

            
        }];


        
    }else{

        UITextField * textField = [HMTextField createTextFieldWithText:nil andpLlaceholder:_dataArray[1][indexPath.section] andTextColor:[UIColor lightGrayColor] andBgColor:nil andFont:[UIFont fontWithName:default_fontname size:autoScaleW(14)] andTextAlignment:NSTextAlignmentLeft andTextFieldViewMode:UITextFieldViewModeWhileEditing];

        textField.tag = indexPath.section ;
        textField.delegate = self;

        [textField addTarget:self action:@selector(changedTextField:) forControlEvents:UIControlEventEditingChanged];


        [cell.contentView  addSubview:textField];

        [textField mas_makeConstraints:^(MASConstraintMaker *make) {

            make.centerY.mas_equalTo(cell.contentView);
            make.left.mas_equalTo(autoScaleW(90));

            make.height.mas_equalTo(autoScaleH(17));

            make.right.mas_equalTo(autoScaleW(-20));
            
        }];

    }


    return cell;


}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{


    return section == 0?0.f:1;

}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return autoScaleH(50);
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    [LZCityPickerController showPickerInViewController:self selectBlock:^(NSString *address, NSString *province, NSString *city, NSString *area) {

        // 选择结果回调
       _location_label.text = address;
        NSLog(@"%@--%@--%@--%@",address,province,city,area);

    }];

}
-(void)clickButton:(UIButton *)button{
    
    switch (button.tag) {
        case 101:
        {

            [self.navigationController popViewControllerAnimated:YES];

        }
            break;

        case 102:
        {

        }
            break;
            
            
        default:
            break;
    }

    
    
}

#pragma mark -给每个cell中的textfield添加事件，只要值改变就调用此函数
-(void)changedTextField:(id)sender
{
    UITextField *tempTextField = (UITextField *)sender;
     NSLog(@"tag------%ld;值是---%@",tempTextField.tag,tempTextField.text);

    if (tempTextField.tag == 0) {

        _name_str = tempTextField.text;

    }else if (tempTextField.tag == 1){

        _phone_num_str = tempTextField.text;

    }else if(tempTextField.tag == 2){

          _location_str = tempTextField.text;

    }else if(tempTextField.tag == 3){

        _location_detail_str = tempTextField.text;

    }


}

- (void)textFieldDidEndEditing:(UITextField *)textField{


  NSLog(@"textField.text ===== %@",textField.text);
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}




@end
